<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="会员有效期">
      {{dataForm.days}}天
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="原价" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="现价" prop="priceFinal">
      <el-input v-model="dataForm.priceFinal" placeholder="最终价格"></el-input>
    </el-form-item>
    <el-form-item label="标签">
      <el-upload
        class="upload-demo"
        :action="uploadImgUrl"
        accept=".jpg, .png, .jpeg"
        :on-remove="handleRemove"
        :on-error="handleImgError"
        :on-success="handleImgSuccess"
        :before-upload="beforeImgUpload"
        :file-list="fileList">
        <el-button size="small" type="primary">添加图片</el-button>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        uploadImgUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        fileList: [],
        dataForm: {
          details: '',
          name: '',
          price: '',
          discount: '',
          priceFinal: '',
          pic: '',
          days: '',
          type: '',
          remark: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '原价不能为空', trigger: 'blur' }
          ],
          priceFinal: [
            { required: true, message: '现价不能为空', trigger: 'blur' }
          ],
          pic: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.fileList = []
        this.pic = ''
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbserviceprice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.pbServicePrice
                if (this.dataForm.pic) {
                  this.fileList = [{'name':this.dataForm.pic}]
                }
              }
            })
          }
        })
      },
      beforeImgUpload (file) {
        if (file.type.indexOf('image') < 0) {
          this.$message.error('图片格式不正确')
          return false
        }
      },
      handleImgSuccess (res, file, fileList) {
        this.dataForm.pic = res.url
        this.fileList = [{'name':res.url}]
      },
      handleImgError (err, file, fileList) {
        this.$message.error('上传失败')
      },
      handleRemove (file, fileList) {
        this.dataForm.pic = ''
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbserviceprice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                pbServicePrice: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
